﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace Hfabase.Configs
{
    class XmlConfig : IConfig
    {
        private XmlDocument xmlDoc = new XmlDocument();
        private string file;

        #region IConfig 成员

        public bool Load(string configFilePath = null)
        {
            this.file = configFilePath == null ? ConfigManager.DefaultConfigFile : configFilePath;
            try
            {
                FileStream fs = new FileStream(file, FileMode.Open);
                this.xmlDoc.Load(fs);
                fs.Close();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        public bool Save(string newConfigFilePath = null)
        {
            try
            {
                this.file = newConfigFilePath == null ? this.file : newConfigFilePath;
                this.xmlDoc.Save(file);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        public bool Restore(string configFilePath = null)
        {
            try
            {
                string filePath = configFilePath == null ? ConfigManager.DefaultConfigFile : configFilePath;
                if (File.Exists(filePath))
                    File.Delete(filePath);
                System.IO.File.AppendAllText(filePath, AppResource.appconfig, Encoding.UTF8);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public string GetNodeValue(string path)
        {
            XmlNode node = this.xmlDoc.SelectSingleNode(path);

            if (node == null)
                return null;

            return node.InnerText;
        }

        public string GetNodeAttrValue(string path, string attrName)
        {
            XmlNode node = this.xmlDoc.SelectSingleNode(path);

            if (node == null)
                return null;

            return node.Attributes[attrName].Value;
        }

        public bool SetNodeValue(string path, string value)
        {
            XmlNode node = this.xmlDoc.SelectSingleNode(path);

            if (node == null)
                return false;
            node.InnerText = value;
            return true;
        }

        public bool SetNodeAttrValue(string path, string attrName, string value)
        {
            XmlNode node = this.xmlDoc.SelectSingleNode(path);

            if (node == null)
                return false;

            node.Attributes[attrName].Value = value;
            return true;
        }

        #endregion
    }
}
